import { message } from 'antd';
import {
  getListPricing,
  getProductList,
  getDetailPricing,
  submitPricing,
  updateSubmitPricing,
} from '@/services/productmanage';
import { matchDict } from '@/utils/utils';

export default {
  state: {
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    formFieldsValue: {},
    expandForm: false,
    productList: [], // 产品列表
    detailInfo: {},
    valuesArr: [
      'pdName',
      'pdCode',
      'pdCate',
    ]
  },
  effects: {
    // 获取产品定价查询列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getListPricing, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取产品数据
    *getProductList({ payload }, { call, put }) {
      const response = yield call(getProductList, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          productList: [],
        },
      });
      if (response.success && response.data) {
        const productList = Array.isArray(response.data) ? response.data : [];
        yield put({
          type: 'updateModelData',
          payload: {
            productList,
          },
        });
      } else {
        message.warning(response.message || '获取产品列表：服务端错误');
      }
    },
    // 详情数据
    *getDetail({ payload }, { call, put }) {
      const response = yield call(getDetailPricing, payload);
      if (response.success && response.data) {
        const data =
          response.data.specifNumVo &&
          response.data.specifNumVo.length &&
          response.data.specifNumVo[0];
        yield put({
          type: 'updateModelData',
          payload: {
            detailInfo: data,
          },
        });
      } else {
        message.warning(response.message || '获取产品定价信息：服务端错误');
      }
    },
    // 提交数据
    *submit(
      {
        payload: { values, pdId },
      },
      { call, select }
    ) {
      const { pdName, priceId = null } = yield select(state => state.pricing.detailInfo);
      const submitData = pdId ? { ...values, pdName, pdId, priceId } : { ...values, pdName };
      const response = yield call(pdId ? updateSubmitPricing : submitPricing, submitData);
      return response;
    },
  },
  reducers: {
    // 获取产品定价查询列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置页签状态
    updateTabListQuery(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 选择产品
    getProductInfo(
      state,
      {
        payload: { detailInfo, dictCode },
      }
    ) {
      const pdCateName = matchDict(dictCode.PD_CATE, detailInfo.pdCate) || '--';
      const billFreqName = matchDict(dictCode.PD_BILL_FREQ, detailInfo.billFreq) || '--';
      return {
        ...state,
        detailInfo: { ...detailInfo, pdCateName, billFreqName },
      };
    },
  },
};
